package com.baidu.controller;

import com.baidu.pojo.Orders;
import com.baidu.pojo.Ticket;
import com.baidu.pojo.User;
import com.baidu.service.OrdersService;
import com.baidu.service.TicketService;
import com.baidu.utils.Pages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/AdminOrder")
public class AdminOrderController {

    @Autowired
    private OrdersService ordersService;
    @Autowired
    private TicketService ticketService;
    //分页查询所有订单信息
    @RequestMapping("/getAllOrders")
    public String getAllOrders(@RequestParam(defaultValue = "1") Integer currentPage,
                            @RequestParam(defaultValue = "3") Integer pageSize,
                            HttpServletRequest request,
                            HttpSession session){
        //创建pages对象
        Pages pages=new Pages();
        //设置当前页
        pages.setCurrentPage(currentPage);
        //设置页面容量
        pages.setPageSize(pageSize);
        //查询总记录数
        int total=ordersService.count();
        //设置总记录数
        pages.setTotalCount(total);
        //将pages对象保存到request中
        request.setAttribute("pages",pages);
        //调用service，获取查询结果
        List<Orders> list = ordersService.getByPage(currentPage,pageSize);
        //将集合保存到request中
        request.setAttribute("list",list);
        return "orderList";
    }

    //添加订单
    @RequestMapping("/addOrders")
    public String addOrders(Orders orders){
        System.out.println("-----------------addUser---------------");
        System.out.println("orders:"+orders);
        Date date = new Date();
        orders.setOtime(date);
        //执行添加
        ordersService.addOrders(orders);

        //重定向到/user/getAllOrders
        return "redirect:/AdminOrder/getAllOrders";
    }

    //根据id删除指定订单
    @RequestMapping("/deletOrderById")
    public String deletById(Integer oid){
        System.out.println("-------------deletById-------------");
        System.out.println("oid:"+oid);

        //执行删除
        ordersService.deleteOrdersById(oid);

        //重定向请求到/user/getAllUser
        return "redirect:/AdminOrder/getAllOrders";
    }


    @RequestMapping("/toAddOrders")
    public String toAddOrders(HttpServletRequest request){
        System.out.println("---------------toAddOrders-------------------");
        List<Ticket> t = ticketService.getAllTicket();
        //将对象保存到request中
        request.setAttribute("ticket1",t);
        System.out.println("t:"+t);
        return "orderAdd";
    }
    //进入编辑页面
    @RequestMapping("/toEditOrders")
    public String toEditOrders(Integer oid,HttpServletRequest request){
        System.out.println("---------------toEditUser-------------------");
        System.out.println("id:"+oid);

        //根据id查询指定的用户信息
        Orders orders=ordersService.getOrdersByOid(oid);

        //将对象保存到request中
        request.setAttribute("order2",orders);
        List<Ticket> t = ticketService.getAllTicket();
        //将对象保存到request中
        request.setAttribute("ticket1",t);

        return "orderUpdate";
    }
    //根据id查询指定的用户信息
    @RequestMapping("/getOrderByTid")
    public String getUserById(Integer tid,HttpServletRequest request){
        System.out.println("------------getUserById-------");
        System.out.println("id:"+tid);

        //根据id查询指定的用户信息
        Orders orders=ordersService.getOrdersByOid(tid);

        //将对象保存到request中
        request.setAttribute("user",orders);
        return "orderDetail";
    }

    //根据id修改
    @RequestMapping("/updateOrders")
    public String updateOrders(Orders orders){
        System.out.println("------------updateUser--------------");
        System.out.println("orders:"+orders);

        //执行修改
        ordersService.updateOrdersByOid(orders);

        return "redirect:/AdminOrder/getAllOrders";
    }
}
